

const {ccclass, property} = cc._decorator;

@ccclass
export default class LoginPanel extends cc.Component {

    @property(cc.EditBox)
    accountbox: cc.EditBox = null;
    
    @property(cc.EditBox)
    passwordbox: cc.EditBox = null;
    @property(cc.Toggle)
    allowxieyi: cc.Toggle = null;
    @property(cc.Toggle)
    remebermima: cc.Toggle = null;

    onEnable(){
        cc.vv.eventCustom.on("openHall", ()=>{
            this.node.removeFromParent();
            this.node.destroy()
        }, this.node); 
    }

    start(){
        let accountpassword = cc.sys.localStorage.getItem("accountpassword")
        if(accountpassword && accountpassword.length>0){
            let  jsondata = JSON.parse(accountpassword);
            this.accountbox.string = jsondata.account || "";
            this.passwordbox.string = jsondata.password || "";
        }
    }
    private onClickLogin(): void {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        if(this.allowxieyi.isChecked == false){
            cc.vv.GLUtils.showTips("请先允许用户协议！");
            return  
        }
		let tempResult: string = ""
        tempResult = this.accountbox.string
        if (!cc.vv.GLUtils.checkEnglishAndNumber(tempResult)) {
            cc.vv.GLUtils.showTips("账号只能输入字母和数字!");
            return
        }
		tempResult = this.passwordbox.string
        if (!cc.vv.GLUtils.checkEnglishAndNumber(tempResult)) {
            cc.vv.GLUtils.showTips("密码只能输入字母和数字!");
            return
        }
       var  len = this.passwordbox.string.length;
        if (len < 8 || len > 16) {
            cc.vv.GLUtils.showTips("请输入8-16位的密码!");
            return
        }

		let data = {
			account: this.accountbox.string,
			password: this.passwordbox.string
		}
        if(this.remebermima.isChecked == true){
            cc.sys.localStorage.setItem("accountpassword",JSON.stringify(data));
        }else{
            cc.sys.localStorage.removeItem("accountpassword");
        }
        cc.vv.GLUtils.saveAccountInfo(data);

		cc.vv.eventCustom.emit('initSelfData')
		cc.vv.GLNet.login_dwc("PASSWD",this.accountbox.string, this.passwordbox.string);
    }
    private onClicback(): void {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        cc.vv.UIMgr.close(cc.vv.GLConfig.dwc_LoginPanel_path, cc.vv.UIMgr.closeType.destory);
    }

}
